$(function () {
    //封装获取用户信息的ajax请求
  
    getuserinfo()
    
    //退出功能
    $('.logout').on('click', function () {
        layer.confirm('确认退出登录吗?', {icon: 3, title:'提示'}, function(index){
            //do something
            localStorage.clear()
            location.href = 'login.html'
            layer.close(index);
          });
    })
})
function getuserinfo() {
    $.ajax({
        type: 'GET',
        url: '/my/userinfo',
        headers: {
            Authorization:localStorage.getItem('token')
        },
        success: function (res) {
            if(!res.data) return
            const name = (res.data.nickname || res.data.username)[0].toUpperCase()
            res.data.textAvaatar = name
            // console.log(res);
            if (res.status === 0) {
                const htmlStr1 = template('templ-header', res)
                $('#header-avatar').html(htmlStr1)
                const htmlStr2 = template('tmpl_side', res)
                $('.user_info_box').html(htmlStr2)
                layui.element.render('nav','header-nav')
            } else {
                layer.msg('获取用户信息失败')
            }
        }
         
    })
}